qx.Class.define("caraffa.forms.BalancesListar",
{
	extend : qx.ui.form.ow.FormTab,
	construct : function ()
	{
		this.base(arguments, "id_balances_listar", "Listar Balances");
		
		this._cmbOS = new qx.ui.form.ow.ComboBox("Obra Social:", "", true);
			this._cmbOS.getLabel().setWidth(70);
		this._cmbPlanes = new qx.ui.form.ow.ComboBox("Plan:", "", true);
			this._cmbPlanes.getLabel().setWidth(70);
		
		this._cmbOperacion = new qx.ui.form.ow.ComboBox("Operaciones:", [{value:"", label:"Todas"}, {value:"I", label:"Ingresos"}, {value:"E", label:"Egresos"}]);
			this._cmbOperacion.getLabel().setWidth(80);
			this._cmbOperacion.getCombo().setWidth(110);
		this._cmbMetodoOp = new qx.ui.form.ow.ComboBox("Metodo de Op:", [{value:"", label:"Todos"}, {value:"C", label:"Cheques"}, {value:"D", label:"Deposito"}, {value:"E", label:"Efectivo"}]);
			this._cmbMetodoOp.getLabel().setWidth(80);
			this._cmbMetodoOp.getCombo().setWidth(110);
		
		this._txdFechaDesde = new qx.ui.form.ow.DateText("Desde:");
			this._txdFechaDesde.getLabel().setWidth(40);
			this._txdFechaDesde.getDateText().setWidth(90);
		this._txdFechaHasta = new qx.ui.form.ow.DateText("Hasta:");
			this._txdFechaHasta.getLabel().setWidth(40);
			this._txdFechaHasta.getDateText().setWidth(90);
		
		this._cmbFecha = new qx.ui.form.ow.ComboBox("Fecha:", [{value:'O', label:"de Operacion"}, {value:'C', label:"Correspondiente a"}]);
			this._cmbFecha.getLabel().setWidth(50);
			this._cmbFecha.getCombo().setWidth(110);
		this._cmbRendidos = new qx.ui.form.ow.ComboBox("Rendidos:", [{value:'N', label:"No"}, {value:'R', label:"Si"}])
			this._cmbRendidos.getLabel().setWidth(50);
			this._cmbRendidos.getCombo().setWidth(110);
			
		this._btnListar = new qx.ui.form.Button("Listar");
			this._btnListar.setWidth(110);
			
		this._btnImprimir = new qx.ui.form.Button("Imprimir");
			this._btnImprimir.setWidth(110);
			
		this._lblTotalIngreso = new qx.ui.basic.Label("<b>TOTAL INGRESO: $ 0.00</b>").set({rich:true});
		this._lblTotalEgreso = new qx.ui.basic.Label("<b>TOTAL EGRESO: $ 0.00</b>").set({rich:true});
		
		var model = new qx.ui.table.model.Simple();
		model.setColumns(["cod_planilla_balance", "Op", "Monto", "Fecha Op", "Corresp a", "Metodo OP", "Observ. Op", "Destino", "Observ. Dest.", "OS - Plan"]);
		
		this._grdBalances = new qx.ui.table.Table(model);
		this._grdBalances.setColumnWidth(0, 30);
		this._grdBalances.setColumnWidth(1, 30);
		this._grdBalances.setColumnWidth(2, 90);
		this._grdBalances.setColumnWidth(3, 90);
		this._grdBalances.setColumnWidth(4, 90);
		this._grdBalances.setColumnWidth(5, 100);
		this._grdBalances.setColumnWidth(6, 230);
		this._grdBalances.setColumnWidth(7, 100);
		this._grdBalances.setColumnWidth(8, 230);
		this._grdBalances.setColumnWidth(9, 250);
		
		this._grdBalances.getTableColumnModel().setColumnVisible(0,false);	
		this._grdBalances.setWidth(970);
		this._grdBalances.setHeight(360);
		this._grdBalances.getSelectionModel().setSelectionMode(qx.ui.table.selection.Model.MULTIPLE_INTERVAL_SELECTION);
		
		this._btnMarcarRendidas = new qx.ui.form.Button("Marcar como Rendidos");
		
		this.add(this._cmbOS, {left:5, top:10});
		this.add(this._cmbPlanes, {left:5, top:40});
		this.add(this._cmbOperacion, {left:300, top:10});
		this.add(this._cmbMetodoOp, {left:300, top:40});
		this.add(this._txdFechaDesde, {left:500, top:10});
		this.add(this._txdFechaHasta, {left:500, top:40});
		this.add(this._cmbFecha, {left:650, top:10});
		this.add(this._cmbRendidos, {left:650, top:40});
		this.add(this._btnImprimir, {left:820, top:10});
		this.add(this._btnListar, {left:820, top:40});
		this.add(this._grdBalances, {left:5, top:80});
		this.add(this._lblTotalIngreso, {left:5, top:450});
		this.add(this._lblTotalEgreso, {left:5, top:470});
		this.add(this._btnMarcarRendidas, {left:800, top:450});
		

		this._initForm();
		this._attachEvents();
	},
	members : 
	{
		_initForm : function ()
		{
			this._txdFechaDesde.getDateText().getChildControl("textfield").setValue("");
			this._txdFechaHasta.getDateText().getChildControl("textfield").setValue("");
			
			var rpc = new qx.io.remote.Rpc().set({timeout:10000, url:"services/", serviceName:"Balances"});
			try
			{
				var result = rpc.callSync("getOS");
				this._cmbOS.setNewValues(result, true);
			}
			catch (ex)
			{
				alert(ex);
			}
		},
		_attachEvents : function ()
		{
			this._cmbOS.addListener("changeValue", function ()
			{
				var params = new Object();
				params.cod_obra_social = this._cmbOS.getValue();
				var rpc = new qx.io.remote.Rpc().set({timeout:10000, url:"services/", serviceName:"Balances"});
				try
				{
					var result = rpc.callSync("getPlanes", params);
					this._cmbPlanes.setNewValues(result, true);
				}
				catch (ex)
				{
					alert(ex);
				}
			}, this);
			this._btnListar.addListener("execute", function ()
			{
				var params = new Object();
				params.os = this._cmbOS.getValue();
				params.plan = this._cmbPlanes.getValue();
				params.op = this._cmbOperacion.getValue();
				params.metodo_op = this._cmbMetodoOp.getValue();
				params.desde = this._txdFechaDesde.getValueToSQL();
				params.hasta = this._txdFechaHasta.getValueToSQL();
				params.fecha = this._cmbFecha.getValue();
				params.rendidos = this._cmbRendidos.getValue();
//				alert(qx.util.Json.stringify(params, true));
				var rpc = new qx.io.remote.Rpc().set({timeout:10000, url:"services/", serviceName:"Balances"});
				try
				{
					var result = rpc.callSync("listarBalances", params);
//					alert(qx.util.Json.stringify(result));
					this._grdBalances.resetSelection();
					this._grdBalances.getTableModel().setData(result.grilla);
					this._lblTotalIngreso.setValue("<b>TOTAL INGRESO: $ " + result.total_i + "</b>");
					this._lblTotalEgreso.setValue("<b>TOTAL EGRESO: $ " + result.total_e + "</b>");
				}
				catch (ex)
				{
					alert(ex);
				}
			}, this);
			
			this._btnImprimir.addListener("execute", function ()
			{
				window.open('reports/ListaBalances.php?os=' + this._cmbOS.getValue() + 
				'&plan=' + this._cmbPlanes.getValue() + 
				'&op=' + this._cmbOperacion.getValue() +
				'&metodo_op=' + this._cmbMetodoOp.getValue() +
				'&desde=' + this._txdFechaDesde.getValueToSQL() +
				'&hasta=' + this._txdFechaHasta.getValueToSQL() +
				'&fecha=' + this._cmbFecha.getValue() +
				'&rendidos=' + this._cmbRendidos.getValue() 
				,'','left=150,top=0,width=810,height=600,scrollbars=1');
			}, this);
			
			this._btnMarcarRendidas.addListener("execute", function ()
			{
				var selection = [];
				var grd = this._grdBalances;
	        	this._grdBalances.getSelectionModel().iterateSelection(function(ind)
	        	{
	          		selection.push(grd.getTableModel().getValue(0, ind));
	        	});
	        	if (selection.length > 0)
	        	{
	        		var rpc = new qx.io.remote.Rpc().set({timeout:10000, url:"services/", serviceName:"Balances"});
					try
					{
						var params = new Object();
						params.selection = selection;
						var result = rpc.callSync("marcarRendidos", params);
						this._btnListar.execute();
					}
					catch (ex)
					{
						alert(ex);
					}
	        	}
	        	else
	        	{
	        		alert("Debe seleccionar los registros que desea marcar como Rendidos!");
	        	}
			}, this);
		}
	}
});
